@app.route("/data")
def get_data():
    host = 'localhost'
    port = 3306  # 替换为你的数据库端口号
    user = 'root'
    password = '123456'
    database = 'studentInfo'
    # 创建数据库连接
    conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
    # 创建游标
    cursor = conn.cursor()
    sql = "SELECT * FROM student;"
    cursor.execute(sql)
    results = cursor.fetchall()

    datas = []
    for result in results:
        class_id, gender, count = result
        gender = "男" if gender == 1 else "女"
        datas.append({
            "value": count,
            "name": str((class_id + "班", gender))
        })
    # 关闭游标和连接
    cursor.close()
    conn.close()
    return datas


@app.route("/")
def handle_mes():
    return render_template("index.html")


if __name__ == '__main__':
    socketio.start_background_task(task)
    socketio.run(app, host='127.0.0.1', port=5000, allow_unsafe_werkzeug=True, debug=True)
